//
//  LatestViewController.m
//  GameBoss
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LatestViewController.h"
#import "AFN_Get.h"
#import "FocusModel.h"
#import "ArticleListModel.h"
#import "TopArticleModel.h"
#import "PictureScrollViewCell.h"
#import "UIImageView+WebCache.h"
#import "MoreCell.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "DataViewController.h"
#import "MoreViewController.h"
#import "EvaluateViewController.h"
#import "OtherViewController.h"


@interface LatestViewController ()
// 装focus的model数组
@property(nonatomic, retain)NSMutableArray *arrScroller;
// 装articleList的数组
@property(nonatomic, retain)NSMutableArray *arrArticleList;
// tableview
@property(nonatomic, retain)UITableView *tableView;

// 第一个轮播图的cell
@property(nonatomic, retain)PictureScrollViewCell *cell;


// 取出model中的图片url 放入数组
@property(nonatomic, retain)NSMutableArray *pictureArr;

// 把model多加2个然后放入数组来传值
@property(nonatomic, retain)NSMutableArray *modelArr;
#pragma mark 抽屉
//View页面
@property(nonatomic, retain) UIView *sideView;
//抽屉数组
@property (nonatomic, retain) NSMutableArray *sideArr;
@property (nonatomic, retain) NSMutableArray *picArr;
@property (nonatomic, assign) BOOL isClick;
@property (nonatomic, retain) UITableView *tableViewLeft;
@end

@implementation LatestViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrScroller = [NSMutableArray array];
        self.arrArticleList = [NSMutableArray array];
        self.pictureArr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
        self.title = @"GameBoss";
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"wuqi3"] forBarMetrics:(UIBarMetricsDefault)];
        self.sideArr = [NSMutableArray arrayWithObjects: @"新游",@"评测", @"网游",@"更多",nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];
    imageView.image = [UIImage imageNamed:@"zaochen"];
    [self.view addSubview:imageView];
    [imageView release];
    
    self.tabBarController.tabBar.hidden = NO;
#pragma mark 抽屉效果

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-gengduo"] style:(UIBarButtonItemStylePlain) target:self action:@selector(clickLeft)];
    
    
#pragma mark 主页面的tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height - (64 + 49)) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self addHeaderNew];
    
#pragma mark 夜间判断
    if ([NightMode shareNightMode].isNight) {
        [[NightMode shareNightMode] loadNightModeInfoFormDisk];
    }
}

#pragma mark 抽屉的点击
/********************点击***********************/
- (void)clickLeft
{
    NSArray *images = @[[UIImage imageNamed:@"zuixin"],[UIImage imageNamed:@"74A5005C-3430-4549-A24B-3564C31B1F2F"],[UIImage imageNamed:@"iconfont-svgmoban64"],[UIImage imageNamed:@"264E3744-2DF8-462F-BB0B-A4C2C58979F6"]];
    NSArray *colors = @[[UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
                        [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
                        [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
                        [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1]];
    RNFrostedSidebar *RNF = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:nil borderColors:colors];
    RNF.borderWidth = 5;
    RNF.delegate = self;
    [RNF show];
}
- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index
{
    if (index == 0 || index == 2)
    {
        MoreViewController *moreVC = [[[MoreViewController alloc] init] autorelease];
        moreVC.title = self.sideArr[index];
        if (index == 0)
        {
            moreVC.num = @"2";
        }else
        {
            moreVC.num = @"6";
        }
        moreVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:moreVC animated:YES];
        [moreVC release];
        [sidebar dismiss];
    }else if (index == 1)
    {
        EvaluateViewController *evaVC = [[[EvaluateViewController alloc] init] autorelease];
        evaVC.title = self.sideArr[index];
        evaVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:evaVC animated:YES];
        [evaVC release];
        [sidebar dismiss];
    }else if (index == 3)
    {
        OtherViewController *otherVC = [[OtherViewController alloc] init] ;
        otherVC.title = self.sideArr[index];
        otherVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:otherVC animated:YES];
        [otherVC release];
        [sidebar dismiss];
    }
}
- (void)addHeaderNew
{    __unsafe_unretained typeof(self)vc = self;
    [self.tableView addHeaderWithCallback:^{
        [self.modelArr removeAllObjects];
        [self.arrScroller removeAllObjects];
//        [self.arrArticleList removeAllObjects];
        [vc loadData];
    } ];
    [self.tableView headerBeginRefreshing];
}
#pragma mark 数据解析
- (void)loadData
{
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:@"http://mrobot.pcgames.com.cn/v3/cms/channels/1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 解析focus
        NSDictionary *dic = [NSDictionary dictionary];
        dic = responseObject;
        NSArray *arr = [NSArray array];
        arr = dic[@"focus"];
        for (NSDictionary *temp in arr) {
            FocusModel *model = [[FocusModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            [self.arrScroller addObject:model];
            //            [self.pictureArr addObject:model.image];
            [model release];
        }
        [self.modelArr addObject:[self.arrScroller lastObject]];
        for (int i = 0; i < self.arrScroller.count; i++) {
            [self.modelArr addObject:self.arrScroller[i]];
        }
        [self.modelArr addObject:[self.arrScroller firstObject]];
        // 解析articleList
        NSArray *articleListArr = [NSArray array];
        articleListArr = dic[@"articleList"];
        for (NSDictionary *temp in articleListArr) {
            ArticleListModel *model = [[ArticleListModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            [self.arrArticleList addObject:model];
            [model release];
        }
        [[DataCache shareDataCache] dropTable:@"latesTable"];
        [[DataCache shareDataCache] createTable:@"latesTable"];
        for (int i=0; i<self.arrArticleList.count; i++) {
            [[DataCache shareDataCache] insertModel:@"latesTable" model:self.arrArticleList[i]];
        }
#pragma mark 重载 reloadData
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
        self.arrArticleList = [[DataCache shareDataCache] selectModel:@"latesTable"];
    }];
}
#pragma mark 返回的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.arrArticleList.count;
}
#pragma mark 重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
            static NSString *reuse = @"reuse1";
            self.cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!self.cell) {
                self.cell = [[PictureScrollViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            }
            for (int i = 0; i < self.modelArr.count; i++) {
                [self.cell.pictureArr addObject:self.modelArr[i]];
            }
            self.cell.backgroundColor = [UIColor clearColor];
            // cell中的model传值, 第四步
            self.cell.target = self;
            return self.cell;
        }else{
            static NSString *reuse = @"reuse2";
            MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[MoreCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: reuse];
            }
            ArticleListModel *model = self.arrArticleList[indexPath.row];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.image]];
            cell.titleL.text = model.title;
            cell.pubDateL.text = model.pubDate;
            cell.imageVS.image = [UIImage imageNamed:@"iconfont-liaotian-2"];
            cell.cmtCountL.text =[NSString stringWithFormat:@"%ld", (long)model.cmtCount];
            cell.backgroundColor = [UIColor clearColor];
            return cell;
        }
    }

// cell中的model传值, 第五步
-(void)didTapScrollViewWith:(FocusModel *)focusModel
{
        DataViewController *dataView = [[DataViewController alloc] init];
        dataView.focus = focusModel;
        dataView.name = focusModel.title;
        [self.navigationController pushViewController:dataView animated:YES];
        [dataView release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200 * Screen.height / 667;
    }
    else{
        return 100 * Screen.height / 667;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
            //            NSLog(@"%f", self.cell.myScrollView.contentOffset.x);
        } else{
            DataViewController *dataVC = [[DataViewController alloc] init];
            ArticleListModel *model = self.arrArticleList[indexPath.row];
            dataVC.name = model.title;
            dataVC.art = model;
            dataVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:dataVC animated:YES];
            [dataVC release];
    }
}
#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_arrScroller release];
    [_arrArticleList release];
    [_tableView release];
    [_pictureArr release];
    [_modelArr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
